package com.example.jpetstoremanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.jpetstoremanager.entity.LineItem;
import com.example.jpetstoremanager.entity.Order;
import com.example.jpetstoremanager.entity.OrderStatus;
import com.example.jpetstoremanager.service.OrderService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderManageController {

    @Autowired
    private OrderService orderService;


    @GetMapping("/orderManage")
    public String orderManage(HttpSession session){
        //首页显示所有订单数据
        //放入orderList信息
        List<Order> orderList = orderService.findAllOrders();
        List<OrderStatus> orderStatusList = orderService.findAllOrderStatus();
        List<Order> newOrderList = new ArrayList<>();
        //放入OrderStatus信息
        for (Order order : orderList) {
            for (OrderStatus orderStatus : orderStatusList) {
                if (order.getOrderId() == orderStatus.getOrderid()) {
                    order.addOrderStatus(orderStatus);
                }
            }
            if(order.getOrderStatus().getStatus()!=null)
                newOrderList.add(order);
        }
        session.setAttribute("orderList",newOrderList);
        return "orderManage";
    }

    @GetMapping("/editOrderStatus")
    public String editOrderStatus(@RequestParam("orderId") int orderId,@RequestParam("orderStatus") String orderStatus){
        System.out.println(orderId+orderStatus);
        orderService.updateStatusByOrderid(orderId,orderStatus);
        System.out.println("订单"+orderId+" 状态修改为："+orderStatus);
        return "redirect:/orderManage";
    }

//    @GetMapping("/orderInfo")
//    public String orderInfo(@RequestParam("orderId") int orderId,HttpSession session){
//        System.out.println("点击详情按钮查看订单："+orderId);
//        session.removeAttribute("LineItemList");
//        List<LineItem> lineItemList = orderService.findLineItemsByOrderId(orderId);
//        session.setAttribute("LineItemList",lineItemList);
//        return "redirect:/orderManage";
//    }

    //订单管理页面点击某个订单详情，将订单LineItemList数据传送到js文件的按钮点击方法里调用渲染详情表单
    @RequestMapping("/orderInfo")
    @ResponseBody
    public List<LineItem> orderInfo(@RequestParam("orderId") int orderId) {
        return orderService.findLineItemsByOrderId(orderId);
    }

    @GetMapping("/deleteOrder")
    public String deleteOrder(@RequestParam("orderId") int orderId,HttpSession session){
        //删除orders中的订单
        orderService.deleteOrderByOrderid(orderId);
        //删除orderStatus中的订单
        orderService.deleteOrderStatusByOrderId(orderId);
        //删除lineItem表单中的订单
        orderService.deleteLineItemByOrderId(orderId);

        //放入orderList信息
        List<Order> orderList = orderService.findAllOrders();
        List<OrderStatus> orderStatusList = orderService.findAllOrderStatus();
        List<Order> newOrderList = new ArrayList<>();
        //放入OrderStatus信息
        for (Order order : orderList) {
            for (OrderStatus orderStatus : orderStatusList) {
                if (order.getOrderId() == orderStatus.getOrderid()) {
                    order.addOrderStatus(orderStatus);
                }
            }
            if(order.getOrderStatus().getStatus()!=null)
                newOrderList.add(order);
        }
        session.setAttribute("orderList",newOrderList);

        return "redirect:/orderManage";
    }

}
